package com.isorf.controller;

import com.github.pagehelper.PageInfo;
import com.isorf.common.service.UploadService;
import com.isorf.pojo.Link;
import com.isorf.service.LinkService;
import com.isorf.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@Api(tags = "友情链接管理")
@RestController
public class LinkController {
    @Autowired
    private LinkService linkService;
    @Autowired
    private UploadService uploadService;
    @Value("${file.upload.path}")     // 从application.yml文件中获取图片上传的路径
    private String path;

    /**
     * 上传图片
     * @param flimage
     * @return
     */
    @PostMapping("/uploadImage")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(
                    name = "flimage",
                    value = "上传的图片文件",
                    required = true,
                    paramType = "formData",
                    dataType = "MultipartFile",
                    dataTypeClass = MultipartFile.class,
                    defaultValue = "")})
    public ResponseResult<String> uploadImage(@RequestPart MultipartFile flimage) {
        String flag = uploadService.uploadImage(path, flimage);
        if (flag!=null) {
            return ResponseResult.success(ResponseResult.SUCCESS, "上传成功", flag);
        }else{
            return ResponseResult.failure(ResponseResult.FAILED, "上传失败");
        }
    }
    /**
     * 新增友情链接
     *
     * 返回值：ResponseResult<Boolean>
     * Mapper层返回值：int
     */
    @PostMapping("/addLink")
    public ResponseResult<Boolean> addLink(@RequestBody Link link){
        //Controller 层调业务逻辑层，业务层的返回值 是Boolean类型的
        Boolean result = linkService.addLink(link);
        if( result){
            return ResponseResult.success(ResponseResult.SUCCESS,"添加成功", result);
        }else{
            return ResponseResult.failure(ResponseResult.FAILED,"添加失败");
        }
    }

    /**
     * 删除友情链接
     * @param id
     * 删除成功返回值：ResponseResult<Boolean>
     * Mapper层返回值：int
     */
    @PostMapping("/deleteLink")
    public ResponseResult<Boolean> deleteLink(int id){
        Boolean result = linkService.deleteLink(id);
        if( result){
            return ResponseResult.success(ResponseResult.SUCCESS,"删除成功", result);
        }else{
            return ResponseResult.failure(ResponseResult.FAILED,"删除失败");
        }
    }

    /**
     * 分页查询
     * @param pageNum 是第几页，pageSize 是每页显示多少条数据
     *                返回值：ResponseResult<PageInfo<Link>>
     *                Service层返回值：PageInfo<Link>
     *                Mapper层返回值：List<Link>
     *
     */
    @GetMapping("/pageList")
    public ResponseResult<PageInfo<Link>> pageList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){
        PageInfo<Link> result = linkService.pageList(pageNum, pageSize);
        if( result==null&&result.getList().size()==0){
            return ResponseResult.failure(ResponseResult.FAILED, "查询友情链接列表失败");
        }else{
            return ResponseResult.success(ResponseResult.SUCCESS,"查询成功", result);
        }
    }


}
